package m.jclecai.com.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.CountDownTimer;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.activity.LottoTrendActivity;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.adapter.BallsAdapter;
import m.jclecai.com.adapter.SelectBoxAdapter;
import m.jclecai.com.adapter.independentLotteryAdapter.PLS_SD_QXCA_LotteryAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.BallItem;
import m.jclecai.com.bean.IssueInfoBean;
import m.jclecai.com.bean.MissDataBean;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.contents.LotteryType;
import m.jclecai.com.inter.IAdapterChange;
import m.jclecai.com.inter.OnItemClick;
import m.jclecai.com.pop.SYXW_Center_PopupWindow;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.MakeRandomUtil;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.ShakeUtils;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.BallGridView;
import m.jclecai.com.view.common.BottomBar;
import m.jclecai.com.view.common.VeticalDrawerLayout;
import q.rorbin.badgeview.Badge;

/**
 * Created by hp on 2017/5/10.
 */

public class BetSSCActivity extends BaseActivity implements View.OnClickListener, IAdapterChange, AdapterView.OnItemClickListener, View.OnTouchListener, SensorEventListener {

    private PopupWindow pop;
    private BallGridView gridview_item_one;
    private BallsAdapter itemOneAdapter;

    private BallGridView gridview_item_two;
    private BallsAdapter itemTwoAdapter;

    private BallGridView gridview_item_three;
    private BallsAdapter itemThreeAdapter;

    private BallGridView gridview_item_four;
    private BallsAdapter itemFourAdapter;

    private BallGridView gridview_item_five;
    private BallsAdapter itemFiveAdapter;

    private TextView tv_position_one;
    private TextView tv_position_two;
    private TextView tv_position_three;
    private TextView tv_position_four;
    private TextView tv_position_five;
    private LinearLayout ll_item_one;
    private LinearLayout ll_item_three;
    private LinearLayout ll_item_two;
    private LinearLayout ll_item_four;
    private LinearLayout ll_item_five;
    private TextView tv_term;
    private TextView tv_date;
    private ImageView iv_add_selector;
    private TextView tv_count;

    //玩法模式
    private int modeState;

    //每个item生成的球数
    private int ballCount;

    //每个item选中的数量
    private int countOne, countTwo, countThree, countFour, countFive, countSix, countSeven;

    //保存的选中数据
    private List<String> codeOne = new LinkedList<>();
    private List<String> codeTwo = new LinkedList<>();
    private List<String> codeThree = new LinkedList<>();
    private List<String> codeFour = new LinkedList<>();
    private List<String> codeFive = new LinkedList<>();
    private List<String> codeSix = new LinkedList<>();
    private List<String> codeSeven = new LinkedList<>();

    private String type;

    private int number;

    private ArrayList<String> ballNumber = new ArrayList<>();
    //玩法及模式
    private ArrayList<String> mode = new ArrayList<>();
    //注数及金额
    private ArrayList<String> numMoney = new ArrayList<>();
    //总金额
    private int totalMoney;
    //总注数
    private int totalNum;
    //判断是否是单选
    private boolean isRadio;
    private String[] modeName;
    private GridView gv_selectBox;
    private SelectBoxAdapter adapter;
    private SYXW_Center_PopupWindow selectBoxPop;
    private String[] bonus;

    private TextView tv_number_money;
    private TextView tv_hint;
    private MissDataBean.Omission omission;
    private BallGridView gridview_item_six;
    private BallsAdapter itemSixAdapter;
    private BallGridView gridview_item_seven;
    private BallsAdapter itemSevenAdapter;
    private LinearLayout ll_item_six;
    private LinearLayout ll_item_seven;
    private TextView tv_position_seven;
    private TextView tv_position_six;
    private RelativeLayout line_one;
    private RelativeLayout line_two;
    private RelativeLayout line_three;
    private RelativeLayout line_four;
    private RelativeLayout line_five;
    private RelativeLayout line_six;
    private RelativeLayout line_seven;
    private IssueInfoBean bean;
    private CountDownTimer timer;

    //=======================
    private ListView mLvHistory;
    private VeticalDrawerLayout mDrawerLayout;
    private View mIvDateUnfold;
    private PLS_SD_QXCA_LotteryAdapter mHistoryAdapter;
    private TextView mTvMissData;
    private BottomBar mBottomBar;
    private MissDataBean mMissData;
    private ShakeUtils mShakeUtils;


    @Override
    protected int getContentView() {
        return R.layout.bet_ssc_activity;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitle("时时彩", R.drawable.gs);
        mShakeUtils.register();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mShakeUtils.unRegister();
    }

    private void getMissData(String type) {
        //showDialog();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type",type);
        HttpManager.postAsyn(Constant_Url.GET_MISS_DATA, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！");
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                try {
                    //捕获一下异常
                    MissDataBean bean = FastOk.get(response, MissDataBean.class);
                    if (bean != null) {
                        setMissData(bean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },paramsMap);
    }

    private void getIssueInfo(String type) {

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type",type);
        HttpManager.postAsyn(Constant_Url.ISSUE_INFO, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！");
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                try {
                    IssueInfoBean bean = FastOk.get(response, IssueInfoBean.class);
                    if (bean != null) {
                        setViewInfo(bean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },paramsMap);

    }

    private void setViewInfo(IssueInfoBean bean) {
        if (bean == null) return;
        try {
            this.bean = bean;
            String term = bean.getTermNo();
            //获取当前时间
            String currentTime = BaseUtils.getCurrentTime();
            //获取截止投注时间和当前时间的差值
            long m = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 2);
            long s = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 3);
            long time = m * 60 + s;
            String str = term.substring(term.length() - 3, term.length());
            tv_term.setText("距" + str + "期截止");

            //startCountDownTime(bean.getDeadLine());
            startCountDownTime(Long.parseLong(bean.getDeadLine()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void startCountDownTime(final long time) {
        timer = new CountDownTimer(time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
               /* String currentTime = BaseUtils.getCurrentTime();
                long m = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 2);
                long s = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 3);
                if (s < 10) {
                    String second = "0" + s;
                    tv_date.setText("0" + m + ":" + second);
                } else {
                    tv_date.setText("0" + m + ":" + s);
                }*/
              //  tv_date.setText(BaseUtils.formatDateLineToString(millisUntilFinished));
                tv_date.setText(Html.fromHtml("<font color=red>" + BaseUtils.formatDateLineToString(millisUntilFinished) + "</font>"));
            }

            @Override
            public void onFinish() {
                //tv_date.setText("已截止");
                getIssueInfo(type);
                //timer.cancel();
            }
        };
        timer.start();// 开始计时
    }


    @Override
    protected void initView() {
        leftOptionText("");
        rightOptionText("", R.drawable.action_menu);

        ImageView iv_shake = (ImageView) findViewById(R.id.iv_shake);
        iv_shake.setOnClickListener(this);

        tv_term = (TextView) findViewById(R.id.tv_term);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_hint = (TextView) findViewById(R.id.tv_hint);

        RelativeLayout rl_add = (RelativeLayout) findViewById(R.id.rl_add);
        rl_add.setOnTouchListener(this);
        iv_add_selector = (ImageView) findViewById(R.id.iv_add_selector);
        tv_count = (TextView) findViewById(R.id.tv_count);
        tv_number_money = (TextView) findViewById(R.id.tv_number_money);

        Button bt_bottom_left = (Button) findViewById(R.id.bt_bottom_left);
        bt_bottom_left.setOnClickListener(this);

        Button bt_bottom_right = (Button) findViewById(R.id.bt_bottom_right);
        bt_bottom_right.setOnTouchListener(this);

        //==========新增===================VeticalLayout
        mLvHistory = ((ListView) findViewById(R.id.lv_history));
        mLvHistory.setDivider(null);
        mDrawerLayout = ((VeticalDrawerLayout) findViewById(R.id.drawer_layout));
        mIvDateUnfold = findViewById(R.id.iv_date_unfold);

        mDrawerLayout.setOnDragStatusChange(new VeticalDrawerLayout.OnDragStatusChange() {
            @Override
            public void onOpen() {

            }

            @Override
            public void onClose() {

            }

            @Override
            public void OnDrag(float percent) {
                if (percent >= 0 && percent <= 1) {
                    Float evaluate = BaseUtils.evaluate(percent, 0, 180);
                    mIvDateUnfold.setRotation(evaluate);
                }
            }
        });
        mIvDateUnfold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.toggle();
            }
        });

        mBottomBar = ((BottomBar) findViewById(R.id.bottom_bar));

        mBottomBar.setLeftButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v;
                String text = textView.getText().toString();
                if ("机选".equals(text)) {
                    //随机选中一个
                    randomSelect();
                    textView.setText("清空");
                } else {
                    //清空逻辑；
                    clearBall();
                    textView.setText("机选");
                }
            }
        });

        mBottomBar.setRightButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里我们要的一个操作就是；
                isTo = false;
                restrict = false;

                isTo = true;        //添加的时候，是否进行一个跳转；
                switch (modeState) {
                    case 1:
                        if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                            toActivity();       //跳转
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                            randomSelect();  //这里表示什么都没有，我们只是会生成一个随机的数
                        } else {
                            restrict = true;     //添加并跳转；
                        }
                        break;
                    case 2:
                        if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                        break;
                    case 3:
                    case 7:
                        if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0 && codeFour.size() == 0 && codeFive.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0 && codeFour.size() == 0 && codeFive.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                        break;
                    //大小单双
                    case 8:
                        if (ballNumber.size() > 0 && codeSix.size() == 0 && codeSeven.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeSix.size() == 0 && codeSeven.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                        break;
                    default:
                        if (ballNumber.size() > 0 && codeOne.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                        break;
                }
                //判断retrict是否为true，如果是的话，就跳转，如果不是的话，就不跳转；
                addAllToBetting();
            }
        });

        mBottomBar.setCenterButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTo = false;
                restrict = true;
                addAllToBetting();
            }
        });

        mBottomBar.setOnDragstateChangeListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                if (dragState == Badge.OnDragStateChangedListener.STATE_SUCCEED) {
                    //这里我们要做的一个操作就是清空投注区
                    clearAllBetting();
                }
            }
        });

        //初始化ball的View;
        initBallView();
    }

    private void initBallView() {

        //item标记提示
        tv_position_one = (TextView) findViewById(R.id.tv_position_one);
        tv_position_two = (TextView) findViewById(R.id.tv_position_two);
        tv_position_three = (TextView) findViewById(R.id.tv_position_three);
        tv_position_four = (TextView) findViewById(R.id.tv_position_four);
        tv_position_five = (TextView) findViewById(R.id.tv_position_five);
        tv_position_six = (TextView) findViewById(R.id.tv_position_six);
        tv_position_seven = (TextView) findViewById(R.id.tv_position_seven);

        line_one = (RelativeLayout) findViewById(R.id.line_one);
        line_two = (RelativeLayout) findViewById(R.id.line_two);
        line_three = (RelativeLayout) findViewById(R.id.line_three);
        line_four = (RelativeLayout) findViewById(R.id.line_four);
        line_five = (RelativeLayout) findViewById(R.id.line_five);
        line_six = (RelativeLayout) findViewById(R.id.line_six);
        line_seven = (RelativeLayout) findViewById(R.id.line_seven);

        ll_item_one = (LinearLayout) findViewById(R.id.ll_item_one);
        ll_item_two = (LinearLayout) findViewById(R.id.ll_item_two);
        ll_item_three = (LinearLayout) findViewById(R.id.ll_item_three);
        ll_item_four = (LinearLayout) findViewById(R.id.ll_item_four);
        ll_item_five = (LinearLayout) findViewById(R.id.ll_item_five);
        ll_item_six = (LinearLayout) findViewById(R.id.ll_item_six);
        ll_item_seven = (LinearLayout) findViewById(R.id.ll_item_seven);

        gridview_item_one = (BallGridView) findViewById(R.id.gridview_item_one);
        itemOneAdapter = new BallsAdapter(this);
        gridview_item_one.setOnItemClickListener(this);

        gridview_item_two = (BallGridView) findViewById(R.id.gridview_item_two);
        itemTwoAdapter = new BallsAdapter(this);
        gridview_item_two.setOnItemClickListener(this);

        gridview_item_three = (BallGridView) findViewById(R.id.gridview_item_three);
        itemThreeAdapter = new BallsAdapter(this);
        gridview_item_three.setOnItemClickListener(this);

        gridview_item_four = (BallGridView) findViewById(R.id.gridview_item_four);
        itemFourAdapter = new BallsAdapter(this);
        gridview_item_four.setOnItemClickListener(this);

        gridview_item_five = (BallGridView) findViewById(R.id.gridview_item_five);
        itemFiveAdapter = new BallsAdapter(this);
        gridview_item_five.setOnItemClickListener(this);

        gridview_item_six = (BallGridView) findViewById(R.id.gridview_item_six);
        itemSixAdapter = new BallsAdapter(this);
        gridview_item_six.setOnItemClickListener(this);

        gridview_item_seven = (BallGridView) findViewById(R.id.gridview_item_seven);
        itemSevenAdapter = new BallsAdapter(this);
        gridview_item_seven.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {

        mShakeUtils = new ShakeUtils(this);
        mShakeUtils.setOnShakeUtilsListener(new ShakeUtils.OnShakeUtilsListener() {
            @Override
            public void onShakedListener() {
                randomSelect();
            }

            @Override
            public void hasNotVibratorListener() {

            }
        });

        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        tv_position_one.setText("个位");
        ll_item_one.setVisibility(View.VISIBLE);
        gridview_item_one.setAdapter(itemOneAdapter);
        gridview_item_two.setAdapter(itemTwoAdapter);
        gridview_item_three.setAdapter(itemThreeAdapter);
        gridview_item_four.setAdapter(itemFourAdapter);
        gridview_item_five.setAdapter(itemFiveAdapter);
        gridview_item_six.setAdapter(itemSixAdapter);
        gridview_item_seven.setAdapter(itemSevenAdapter);

        initPopupView();
        initSelectBox();
        //初始化球列表；
        initBallList();
        getMissData(type);
        getIssueInfo(type);
    }

    private void initPopupView() {
        View pv = View.inflate(this, R.layout.lottery_help_popupview, null);
        pop = new PopupWindow(pv, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv_trendChart = (TextView) pv.findViewById(R.id.tv_trendChart);
        tv_trendChart.setOnClickListener(this);
        TextView tv_lotteryHistory = (TextView) pv.findViewById(R.id.tv_kaijiang);

        pv.findViewById(R.id.tv_play_desc).setOnClickListener(this);
        //在这里我们将设置遗漏显示；

        //这里我们要设置显示遗漏的地方；

        mTvMissData = (TextView) pv.findViewById(R.id.tv_missData);
        mTvMissData.setOnClickListener(this);

        boolean showMissing = PreferenceUtil.isShowMissing(this);
        if (showMissing) {
            //这里表示显示；
            showMissData();
        } else {
            hideMissData();
        }


        tv_lotteryHistory.setOnClickListener(this);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
    }

    private void showView(boolean one, boolean two, boolean three, boolean four, boolean five, boolean six, boolean seven) {
        if (one) {
            ll_item_one.setVisibility(View.VISIBLE);
            line_one.setVisibility(View.VISIBLE);
        } else {
            ll_item_one.setVisibility(View.GONE);
            line_one.setVisibility(View.GONE);
        }

        if (two) {
            ll_item_two.setVisibility(View.VISIBLE);
            line_two.setVisibility(View.VISIBLE);
        } else {
            ll_item_two.setVisibility(View.GONE);
            line_two.setVisibility(View.GONE);
        }

        if (three) {
            ll_item_three.setVisibility(View.VISIBLE);
            line_three.setVisibility(View.VISIBLE);
        } else {
            ll_item_three.setVisibility(View.GONE);
            line_three.setVisibility(View.GONE);
        }

        if (four) {
            ll_item_four.setVisibility(View.VISIBLE);
            line_four.setVisibility(View.VISIBLE);
        } else {
            ll_item_four.setVisibility(View.GONE);
            line_four.setVisibility(View.GONE);
        }

        if (five) {
            ll_item_five.setVisibility(View.VISIBLE);
            line_five.setVisibility(View.VISIBLE);
        } else {
            ll_item_five.setVisibility(View.GONE);
            line_five.setVisibility(View.GONE);
        }

        if (six) {
            ll_item_six.setVisibility(View.VISIBLE);
            line_six.setVisibility(View.VISIBLE);
        } else {
            ll_item_six.setVisibility(View.GONE);
            line_six.setVisibility(View.GONE);
        }

        if (seven) {
            ll_item_seven.setVisibility(View.VISIBLE);
            line_seven.setVisibility(View.VISIBLE);
        } else {
            ll_item_seven.setVisibility(View.GONE);
            line_seven.setVisibility(View.GONE);
        }
    }


    private void initSelectBox() {
        selectBoxPop = new SYXW_Center_PopupWindow(this);
        selectBoxPop.setContent(R.layout.select_box_grid_ssc);

        modeName = new String[]{"一星直选", "二星直选", "三星直选", "五星直选", "二星组选", "组选三", "组选六", "五星通选", "大小单双"};
        bonus = new String[]{"奖金10元", "奖金100元", "奖金1000元", "奖金100000元", "奖金50元", "奖金320元", "奖金160元", "奖金20440元", "奖金4元"};
        View view = selectBoxPop.getView();
        gv_selectBox = (GridView) view.findViewById(R.id.gv_selectBox);
        int w = BaseUtils.getDisplaySize(this, "width");
        int width = w / 3;
        gv_selectBox.setColumnWidth(width);
        adapter = new SelectBoxAdapter(this, new OnItemClick() {
            @Override
            public void onItemClick(String mode) {
                modeState = stringOfInt(mode);
                switch (modeState) {
                    case 0:
                        //  switchData(null);
                        tv_hint.setText("至少选1个号,选号与开奖个位一致中10元");
                        tv_position_one.setText("个位");
                        showView(true, false, false, false, false, false, false);
                        //设置相应的遗漏；
                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM4());
                        itemOneAdapter.notifyDataSetChanged();
                        break;
                    case 1:
                        // switchData(null);
                        tv_position_one.setText("十位");
                        tv_position_two.setText("个位");
                        tv_hint.setText("每位至少选1个号,按位猜对开奖后2位中100元");
                        showView(true, true, false, false, false, false, false);
                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM3());
                        itemTwoAdapter.addMissDataInfo(mMissData.getOmission().getM4());

                        itemOneAdapter.notifyDataSetChanged();
                        itemTwoAdapter.notifyDataSetChanged();
                        break;
                    case 2:
                        // switchData(null);
                        tv_position_one.setText("百位");
                        tv_position_two.setText("十位");
                        tv_position_three.setText("个位");
                        tv_hint.setText("每位至少选1个号,按位猜对开奖后3位中1000元");
                        showView(true, true, true, false, false, false, false);

                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM2());
                        itemTwoAdapter.addMissDataInfo(mMissData.getOmission().getM3());
                        itemThreeAdapter.addMissDataInfo(mMissData.getOmission().getM4());

                        itemOneAdapter.notifyDataSetChanged();
                        itemTwoAdapter.notifyDataSetChanged();
                        itemThreeAdapter.notifyDataSetChanged();
                        break;
                    case 3:
                        // switchData(null);
                        tv_position_one.setText("万位");
                        tv_position_two.setText("千位");
                        tv_position_three.setText("百位");
                        tv_position_four.setText("十位");
                        tv_position_five.setText("个位");
                        tv_hint.setText("每位至少选1个号,按位猜对开奖号中100000元");
                        showView(true, true, true, true, true, false, false);

                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM0());
                        itemTwoAdapter.addMissDataInfo(mMissData.getOmission().getM1());
                        itemThreeAdapter.addMissDataInfo(mMissData.getOmission().getM2());
                        itemFourAdapter.addMissDataInfo(mMissData.getOmission().getM3());
                        itemFiveAdapter.addMissDataInfo(mMissData.getOmission().getM4());

                        itemOneAdapter.notifyDataSetChanged();
                        itemTwoAdapter.notifyDataSetChanged();
                        itemThreeAdapter.notifyDataSetChanged();
                        itemFourAdapter.notifyDataSetChanged();
                        itemFiveAdapter.notifyDataSetChanged();
                        break;
                    case 4:
                        //switchData(null);
                        tv_position_one.setText("选号");
                        tv_hint.setText("选2~6个号,奖金50元");
                        ll_item_one.setVisibility(View.VISIBLE);
                        showView(true, false, false, false, false, false, false);
                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM5());
                        itemOneAdapter.notifyDataSetChanged();
                        break;
                    case 5:
                        // switchData(null);
                        tv_position_one.setText("选号");
                        tv_hint.setText("至少选2个号,奖金320元");
                        showView(true, false, false, false, false, false, false);

                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM6());
                        itemOneAdapter.notifyDataSetChanged();
                        break;
                    case 6:
                        // switchData(null);
                        tv_position_one.setText("选号");
                        tv_hint.setText("至少选3个号,奖金160元");
                        showView(true, false, false, false, false, false, false);
                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM6());
                        itemOneAdapter.notifyDataSetChanged();
                        break;
                    case 7: //五星通选
                        //switchData(null);
                        tv_position_one.setText("万位");
                        tv_position_two.setText("千位");
                        tv_position_three.setText("百位");
                        tv_position_four.setText("十位");
                        tv_position_five.setText("个位");
                        tv_hint.setText("每位至少选1个号,最高奖20440元");
                        showView(true, true, true, true, true, false, false);

                        itemOneAdapter.addMissDataInfo(mMissData.getOmission().getM0());
                        itemTwoAdapter.addMissDataInfo(mMissData.getOmission().getM1());
                        itemThreeAdapter.addMissDataInfo(mMissData.getOmission().getM2());
                        itemFourAdapter.addMissDataInfo(mMissData.getOmission().getM3());
                        itemFiveAdapter.addMissDataInfo(mMissData.getOmission().getM4());

                        itemOneAdapter.notifyDataSetChanged();
                        itemTwoAdapter.notifyDataSetChanged();
                        itemThreeAdapter.notifyDataSetChanged();
                        itemFourAdapter.notifyDataSetChanged();
                        itemFiveAdapter.notifyDataSetChanged();
                        break;
                    case 8: //大小单双
                        // switchData(null);
                        tv_position_six.setText("十位");
                        tv_position_seven.setText("个位");
                        tv_hint.setText("猜后两位大小单双,奖金4元");
                        showView(false, false, false, false, false, true, true);
                        break;
                }
                selectBoxPop.dismiss();
            }
        });
        adapter.setData(modeName, bonus);
        gv_selectBox.setAdapter(adapter);
        //selectBoxPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        selectBoxPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                startAnimation(ivTitleIcon, R.anim.rotate_180_360);
            }
        });
    }

    /**
     * 设置遗漏和历史记录；
     */
    public void setMissData(MissDataBean missData) {

        mMissData = missData;
        //这里的话，我们是需要设置遗漏和最近10期的开奖情况；
        mHistoryAdapter = new PLS_SD_QXCA_LotteryAdapter(this);
        mHistoryAdapter.setData(missData.getCodes());
        mLvHistory.setAdapter(mHistoryAdapter);
        //这里的话，我们要做的一个操作就是测量

        int height = measureListViewWrongHeight(mLvHistory);

        mLvHistory.getLayoutParams().height = height;
        mLvHistory.requestLayout();
        mDrawerLayout.measureInnerLayout();

        itemOneAdapter.addMissDataInfo(missData.getOmission().getM4());


        itemSixAdapter.addMissDataInfo(missData.getOmission().getM9());
        itemSevenAdapter.addMissDataInfo(missData.getOmission().getM10());

        itemOneAdapter.notifyDataSetChanged();

        itemSixAdapter.notifyDataSetChanged();
        itemSevenAdapter.notifyDataSetChanged();
    }

    /**
     * 这里我们要做的一个操作就是显示小球的列表
     */
    public void initBallList() {
        itemOneAdapter.removeItem();
        itemTwoAdapter.removeItem();
        itemThreeAdapter.removeItem();
        itemFourAdapter.removeItem();
        itemFiveAdapter.removeItem();
        itemSixAdapter.removeItem();
        itemSevenAdapter.removeItem();


        int one = 0;
        int two = 0;
        int three = 0;
        int four = 0;
        int five = 0;

        BallItem ballItem = null;

        while (true) {
            if (one > 9) {
                break;
            }
            ballItem = new BallItem("" + one, false, true);
            itemOneAdapter.addItem(ballItem);
            one++;
        }
        while (true) {
            if (two > 9) {
                break;
            }
            ballItem = new BallItem("" + two, false, true);
            itemTwoAdapter.addItem(ballItem);
            two++;
        }
        while (true) {
            if (three > 9) {
                break;
            }
            ballItem = new BallItem("" + three, false, true);
            itemThreeAdapter.addItem(ballItem);
            three++;
        }
        while (true) {
            if (four > 9) {
                break;
            }
            ballItem = new BallItem("" + four, false, true);
            itemFourAdapter.addItem(ballItem);
            four++;
        }

        while (true) {
            if (five > 9) {
                break;
            }
            ballItem = new BallItem("" + five, false, true);
            itemFiveAdapter.addItem(ballItem);
            five++;
        }

        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                ballItem = new BallItem("大", false, true);
            } else if (i == 1) {
                ballItem = new BallItem("小", false, true);
            } else if (i == 2) {
                ballItem = new BallItem("单", false, true);
            } else {
                ballItem = new BallItem("双", false, true);
            }
            itemSixAdapter.addItem(ballItem);
        }
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                ballItem = new BallItem("大", false, true);
            } else if (i == 1) {
                ballItem = new BallItem("小", false, true);
            } else if (i == 2) {
                ballItem = new BallItem("单", false, true);
            } else {
                ballItem = new BallItem("双", false, true);
            }
            itemSevenAdapter.addItem(ballItem);
        }

        itemOneAdapter.notifyDataSetChanged();
        itemTwoAdapter.notifyDataSetChanged();
        itemThreeAdapter.notifyDataSetChanged();
        itemFourAdapter.notifyDataSetChanged();
        itemFiveAdapter.notifyDataSetChanged();

        itemSixAdapter.notifyDataSetChanged();
        itemSevenAdapter.notifyDataSetChanged();
        line_one.setVisibility(View.VISIBLE);
    }

/*
    //切换数据
    private void switchData(MissDataBean bean) {
//        if (bean == null) return;
        itemOneAdapter.removeItem();
        itemTwoAdapter.removeItem();
        itemThreeAdapter.removeItem();
        itemFourAdapter.removeItem();
        itemFiveAdapter.removeItem();
        itemSixAdapter.removeItem();
        itemSevenAdapter.removeItem();

        if (bean != null) {
            omission = bean.getOmission();
        }
//        if (omission == null) return;

        int one = 0;
        int two = 0;
        int three = 0;
        int four = 0;
        int five = 0;

        BallItem ballItem = null;
        String[] m0 = omission.getM0().split(",");
        String[] m1 = omission.getM0().split(",");
        String[] m2 = omission.getM0().split(",");
        String[] m3 = omission.getM0().split(",");
        String[] m4 = omission.getM0().split(",");
        String[] m5 = omission.getM0().split(",");

        //大小单双遗漏
        String[] m9 = omission.getM0().split(",");
        String[] m10 = omission.getM0().split(",");

        switch (modeState) {
            //一星直选
            case 0:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                break;
            //二星直选
            case 1:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                while (true) {
                    if (two > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + two, false, true, m1[two]);
                    itemTwoAdapter.addItem(ballItem);
                    two++;
                }
                itemTwoAdapter.notifyDataSetChanged();
                break;
            //三星直选
            case 2:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                while (true) {
                    if (two > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + two, false, true, m1[two]);
                    itemTwoAdapter.addItem(ballItem);
                    two++;
                }
                itemTwoAdapter.notifyDataSetChanged();
                while (true) {
                    if (three > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + three, false, true, m2[three]);
                    itemThreeAdapter.addItem(ballItem);
                    three++;
                }
                itemThreeAdapter.notifyDataSetChanged();
                break;
            case 3:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                while (true) {
                    if (two > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + two, false, true, m1[two]);
                    itemTwoAdapter.addItem(ballItem);
                    two++;
                }
                itemTwoAdapter.notifyDataSetChanged();
                while (true) {
                    if (three > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + three, false, true, m2[three]);
                    itemThreeAdapter.addItem(ballItem);
                    three++;
                }
                itemThreeAdapter.notifyDataSetChanged();
                while (true) {
                    if (four > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + four, false, true, m3[four]);
                    itemFourAdapter.addItem(ballItem);
                    four++;
                }
                itemFourAdapter.notifyDataSetChanged();
                while (true) {
                    if (five > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + five, false, true, m4[five]);
                    itemFiveAdapter.addItem(ballItem);
                    five++;
                }
                itemFiveAdapter.notifyDataSetChanged();
                break;
            //五星通选
            case 7:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                while (true) {
                    if (two > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + two, false, true, m1[two]);
                    itemTwoAdapter.addItem(ballItem);
                    two++;
                }
                itemTwoAdapter.notifyDataSetChanged();
                while (true) {
                    if (three > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + three, false, true, m2[three]);
                    itemThreeAdapter.addItem(ballItem);
                    three++;
                }
                itemThreeAdapter.notifyDataSetChanged();
                while (true) {
                    if (four > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + four, false, true, m3[four]);
                    itemFourAdapter.addItem(ballItem);
                    four++;
                }
                itemFourAdapter.notifyDataSetChanged();

                while (true) {
                    if (five > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + five, false, true, m4[five]);
                    itemFiveAdapter.addItem(ballItem);
                    five++;
                }
                itemFiveAdapter.notifyDataSetChanged();
                break;
            case 8:
                for (int i = 0; i < 4; i++) {
                    if (i == 0) {
                        ballItem = new BallItem("大", false, true, m9[0]);
                    } else if (i == 1) {
                        ballItem = new BallItem("小", false, true, m9[1]);
                    } else if (i == 2) {
                        ballItem = new BallItem("单", false, true, m9[2]);
                    } else {
                        ballItem = new BallItem("双", false, true, m9[3]);
                    }
                    itemSixAdapter.addItem(ballItem);
                }
                for (int i = 0; i < 4; i++) {
                    if (i == 0) {
                        ballItem = new BallItem("大", false, true, m10[0]);
                    } else if (i == 1) {
                        ballItem = new BallItem("小", false, true, m10[1]);
                    } else if (i == 2) {
                        ballItem = new BallItem("单", false, true, m10[2]);
                    } else {
                        ballItem = new BallItem("双", false, true, m10[3]);
                    }
                    itemSevenAdapter.addItem(ballItem);
                }
                itemSixAdapter.notifyDataSetChanged();
                itemSevenAdapter.notifyDataSetChanged();
                break;
            default:
                while (true) {
                    if (one > 9) {
                        break;
                    }
                    ballItem = new BallItem("" + one, false, true, m0[one]);
                    itemOneAdapter.addItem(ballItem);
                    one++;
                }
                itemOneAdapter.notifyDataSetChanged();
                break;
        }
    }*/


    private int stringOfInt(String mode) {

        if (mode.equals("一星直选")) {
            return 0;
        } else if (mode.equals("二星直选")) {
            return 1;
        } else if (mode.equals("三星直选")) {
            return 2;
        } else if (mode.equals("五星直选")) {
            return 3;
        } else if (mode.equals("二星组选")) {
            return 4;
        } else if (mode.equals("组选三")) {
            return 5;
        } else if (mode.equals("组选六")) {
            return 6;
        } else if (mode.equals("五星通选")) {
            return 7;
        } else if (mode.equals("大小单双")) {
            return 8;
        } else {
            return -1;
        }
    }

    @OnClick({R.id.tv_mid_title, R.id.iv_title_icon})
    void showLotteryMenu() {
        startAnimation(ivTitleIcon, R.anim.rotate_0_180);
        height = rlBar.getHeight();
        statusHeight = BaseUtils.getStatusHeight(this);
        // selectBoxPop.showAtLocation(rl_title, 0, 0, height + statusHeight);
        selectBoxPop.showAsDropDown(rlBar);
    }

    @OnClick(R.id.tv_right_option)
    void showHelpMenu() {
        height = rlBar.getHeight();
        statusHeight = BaseUtils.getStatusHeight(this);
        int width = BaseUtils.getDisplaySize(this, "width");
        pop.showAtLocation(rlBar, 0, width, height + statusHeight);
    }

    int height, statusHeight;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_bottom_left:
                clearBall();
                break;
            case R.id.iv_shake:
                randomSelect();
                break;
            case R.id.tv_missData:
                //这里我们要做的一个操作就是
                boolean showMissing = PreferenceUtil.isShowMissing(this);
                if (showMissing) {
                    hideMissData();
                } else {
                    showMissData();
                }
                pop.dismiss();
                break;
            case R.id.tv_play_desc:
                Intent helpIntent = new Intent(this, WebHelperActivity.class);
                helpIntent.putExtra("title", "玩法介绍");
                helpIntent.putExtra("code", type);
                helpIntent.putExtra("isLocal", true);
                startActivity(helpIntent);
                break;
            case R.id.tv_trendChart:
                Intent intent1 = new Intent(this, LottoTrendActivity.class);
                intent1.putExtra("title","重庆时时彩");
                intent1.putExtra("lotteryId", LotteryType.CQSSC);
                startActivity(intent1);
                break;
            case R.id.tv_kaijiang:
                //这里是历史开奖的数据
                Intent intent = new Intent(this, LotteryHistoryActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("typeName","重庆时时彩");
                startActivity(intent);
                break;
        }
    }

    @Override
    public View onItemChanged(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        BallItem ballItem = null;
        boolean selected = false;
        String num = null;
        String missData = null;
        int mViewGroup = 0;

        BallsAdapter ballsAdapter = null;

        if (parent.getId() == R.id.gridview_item_one) {
            ballItem = (BallItem) itemOneAdapter.getItem(position);

            ballsAdapter = itemOneAdapter;
        } else if (parent.getId() == R.id.gridview_item_two) {
            ballItem = (BallItem) itemTwoAdapter.getItem(position);

            ballsAdapter = itemTwoAdapter;
        } else if (parent.getId() == R.id.gridview_item_three) {
            ballItem = (BallItem) itemThreeAdapter.getItem(position);

            ballsAdapter = itemThreeAdapter;
        } else if (parent.getId() == R.id.gridview_item_four) {
            ballItem = (BallItem) itemFourAdapter.getItem(position);

            ballsAdapter = itemFourAdapter;
        } else if (parent.getId() == R.id.gridview_item_five) {
            ballItem = (BallItem) itemFiveAdapter.getItem(position);

            ballsAdapter = itemFiveAdapter;
        } else if (parent.getId() == R.id.gridview_item_six) {
            ballItem = (BallItem) itemSixAdapter.getItem(position);

            ballsAdapter = itemSixAdapter;
        } else if (parent.getId() == R.id.gridview_item_seven) {
            ballItem = (BallItem) itemSevenAdapter.getItem(position);

            ballsAdapter = itemSevenAdapter;
        }

        mViewGroup = R.layout.ballgridview_ball_item;

        if (null == convertView) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(this);
            //convertView = inflater.inflate(mViewGroup, parent, true);
            convertView = inflater.inflate(mViewGroup, parent, false);

            holder.ball_num = (TextView) convertView.findViewById(R.id.txt_ball_num);
            holder.tv_missData = (TextView) convertView.findViewById(R.id.tv_missData);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //是否选中
        selected = ballItem.isSelected();
        num = ballItem.getBallNum();
        //设置数字
        holder.ball_num.setText(num);

        //遗漏数据
        if (ballsAdapter.getMissDataInfo().length > 0) {
            holder.tv_missData.setText(ballsAdapter.getMissDataInfo()[position]);
        } else {
            holder.tv_missData.setText("-");
        }

        if (ballsAdapter.isShowMissData()) {
            holder.tv_missData.setVisibility(View.VISIBLE);
        } else {
            holder.tv_missData.setVisibility(View.GONE);
        }

        //判断球是否被选中
        if (selected) {
            holder.ball_num.setBackgroundResource(R.drawable.red_ball);
            holder.ball_num.setTextColor(Color.WHITE);
        } else {
            holder.ball_num.setBackgroundResource(R.drawable.shape_ball_red_bg);
            holder.ball_num.setTextColor(getResources().getColor(R.color.baseRed));
        }

        //这里的话，我们要做的一个操作就是判断；


        return convertView;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int sensorType = event.sensor.getType();
        float[] values = event.values;
        if (sensorType == Sensor.TYPE_ACCELEROMETER) {
            if ((Math.abs(values[0]) > 14 || Math.abs(values[1]) > 14 || Math.abs(values[2]) > 14)) {
                randomSelect();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


    private class ViewHolder {
        TextView ball_num;
        TextView tv_missData;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getId() == R.id.gridview_item_one) {
            BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(position);
            if (ballItemOne.isSelected()) {
                ballItemOne.setSelected(false);
                countOne--;
                codeOne.remove(ballItemOne.getBallNum());
            } else {
                if (codeOne.size() == 6 && modeState == 4) {
                    ToastUtil.showToast(this, "二星组选最多只能选择6个号码");
                } else if (codeOne.size() == 7 && modeState == 6) {
                    ToastUtil.showToast(this, "组三最多只能选择7个号码");
                } else {
                    ballItemOne.setSelected(true);
                    countOne++;
                    codeOne.add(ballItemOne.getBallNum());
                }
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemOneAdapter.notifyDataSetChanged();
        } else if (parent.getId() == R.id.gridview_item_two) {
            BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(position);
            if (ballItemTwo.isSelected()) {
                ballItemTwo.setSelected(false);
                countTwo--;
                codeTwo.remove(ballItemTwo.getBallNum());
            } else {
                ballItemTwo.setSelected(true);
                countTwo++;
                codeTwo.add(ballItemTwo.getBallNum());
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemTwoAdapter.notifyDataSetChanged();

        } else if (parent.getId() == R.id.gridview_item_three) {
            BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(position);
            if (!ballItemThree.isSelected()) {
                ballItemThree.setSelected(true);
                countThree++;
                codeThree.add(ballItemThree.getBallNum());
            } else {
                ballItemThree.setSelected(false);
                countThree--;
                codeThree.remove(ballItemThree.getBallNum());
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemThreeAdapter.notifyDataSetChanged();
        } else if (parent.getId() == R.id.gridview_item_four) {
            BallItem ballItemFour = (BallItem) itemFourAdapter.getItem(position);
            if (!ballItemFour.isSelected()) {
                ballItemFour.setSelected(true);
                countFour++;
                codeFour.add(ballItemFour.getBallNum());
            } else {
                ballItemFour.setSelected(false);
                countFour--;
                codeFour.remove(ballItemFour.getBallNum());
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemFourAdapter.notifyDataSetChanged();
        } else if (parent.getId() == R.id.gridview_item_five) {
            BallItem ballItemFive = (BallItem) itemFiveAdapter.getItem(position);
            if (!ballItemFive.isSelected()) {
                ballItemFive.setSelected(true);
                countFive++;
                codeFive.add(ballItemFive.getBallNum());
            } else {
                ballItemFive.setSelected(false);
                countFive--;
                codeFive.remove(ballItemFive.getBallNum());
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemFiveAdapter.notifyDataSetChanged();
            //TODO 大小单双item
        } else if (parent.getId() == R.id.gridview_item_six) {
            BallItem ballItemSix = (BallItem) itemSixAdapter.getItem(position);
            if (!ballItemSix.isSelected()) {
                for (int i = 0; i < itemSixAdapter.getCount(); i++) {
                    BallItem item = (BallItem) itemSixAdapter.getItem(i);
                    item.setSelected(false);
                    codeSix.remove(item.getBallNum());
                    countSix = 0;
                }
                ballItemSix.setSelected(true);
                countSix++;
                codeSix.add(ballItemSix.getBallNum());
            } else {
                ballItemSix.setSelected(false);
                countSix--;
                codeSix.remove(ballItemSix.getBallNum());
            }
            Log.e(TAG, "ballItemSix" + ballItemSix.getBallNum());
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemSixAdapter.notifyDataSetChanged();
        } else if (parent.getId() == R.id.gridview_item_seven) {
            BallItem ballItemSeven = (BallItem) itemSevenAdapter.getItem(position);
            if (!ballItemSeven.isSelected()) {
                for (int i = 0; i < itemSevenAdapter.getCount(); i++) {
                    BallItem item = (BallItem) itemSevenAdapter.getItem(i);
                    item.setSelected(false);
                    codeSeven.remove(item.getBallNum());
                    countSeven = 0;
                }
                ballItemSeven.setSelected(true);
                countSeven++;
                codeSeven.add(ballItemSeven.getBallNum());
            } else {
                ballItemSeven.setSelected(false);
                countSeven--;
                codeSeven.remove(ballItemSeven.getBallNum());
            }
            setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
            itemSevenAdapter.notifyDataSetChanged();
        }


        //判断一下是否有选中的如果有的话，我们就变成清空；
        if (codeOne.size() > 0 || codeTwo.size() > 0
                || codeThree.size() > 0 || codeFour.size() > 0
                || codeFive.size() > 0 || codeSix.size() > 0
                || codeSeven.size() > 0
                ) {
            mBottomBar.setLeftText("清空");
        } else {
            mBottomBar.setLeftText("机选");
        }
    }

    private List<String> randomBallOne = new LinkedList<>();
    private List<String> randomBallTwo = new LinkedList<>();
    private List<String> randomBallThree = new LinkedList<>();
    private List<String> randomBallFour = new LinkedList<>();
    private List<String> randomBallFive = new LinkedList<>();
    //大小单双
    private List<String> randomBallSix = new LinkedList<>();
    private List<String> randomBallSeven = new LinkedList<>();

    String[] str = new String[]{"大", "小", "单", "双"};

    //机选
    private void randomSelect() {
        clearBall();
        switch (modeState) {
            //一星直选
            case 0:
                randomBallOne = MakeRandomUtil.getBall(9);
                break;
            //二星直选
            case 1:
                randomBallOne = MakeRandomUtil.getBall(9);
                randomBallTwo = MakeRandomUtil.getBall(9);
                break;
            //三星直选
            case 2:
                randomBallOne = MakeRandomUtil.getBall(9);
                randomBallTwo = MakeRandomUtil.getBall(9);
                randomBallThree = MakeRandomUtil.getBall(9);
                break;
            //五星直选
            case 3:
                randomBallOne = MakeRandomUtil.getBall(9);
                randomBallTwo = MakeRandomUtil.getBall(9);
                randomBallThree = MakeRandomUtil.getBall(9);
                randomBallFour = MakeRandomUtil.getBall(9);
                randomBallFive = MakeRandomUtil.getBall(9);
                break;
            //二星组选
            case 4:
                randomBallOne = MakeRandomUtil.getWeiBall(9, 2);
                break;
            //组选三
            case 5:
                randomBallOne = MakeRandomUtil.getWeiBall(9, 2);
                break;
            //组选六
            case 6:
                Log.e(TAG, "组选六");
                randomBallOne = MakeRandomUtil.getWeiBall(9, 3);
                break;
            //五星通选
            case 7:
                randomBallOne = MakeRandomUtil.getBall(9);
                randomBallTwo = MakeRandomUtil.getBall(9);
                randomBallThree = MakeRandomUtil.getBall(9);
                randomBallFour = MakeRandomUtil.getBall(9);
                randomBallFive = MakeRandomUtil.getBall(9);
                break;
            //大小单双
            case 8:
                randomBallSix = MakeRandomUtil.getBall(4);
                randomBallSeven = MakeRandomUtil.getBall(4);
                break;
        }

        for (int i = 0; i < itemOneAdapter.getCount(); i++) {
            BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
            for (int j = 0; j < randomBallOne.size(); j++) {
                if (ballItemOne.getBallNum().equals(randomBallOne.get(j))) {
                    ballItemOne.setSelected(true);
                    codeOne.add(randomBallOne.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < itemTwoAdapter.getCount(); i++) {
            BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(i);
            for (int j = 0; j < randomBallTwo.size(); j++) {
                if (ballItemTwo.getBallNum().equals(randomBallTwo.get(j))) {
                    ballItemTwo.setSelected(true);
                    codeTwo.add(randomBallTwo.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < itemThreeAdapter.getCount(); i++) {
            BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(i);
            for (int j = 0; j < randomBallThree.size(); j++) {
                if (ballItemThree.getBallNum().equals(randomBallThree.get(j))) {
                    ballItemThree.setSelected(true);
                    codeThree.add(randomBallThree.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < itemFourAdapter.getCount(); i++) {
            BallItem ballItemFour = (BallItem) itemFourAdapter.getItem(i);
            for (int j = 0; j < randomBallFour.size(); j++) {
                if (ballItemFour.getBallNum().equals(randomBallFour.get(j))) {
                    ballItemFour.setSelected(true);
                    codeFour.add(randomBallFour.get(j));
                    break;
                }
            }
        }

        for (int i = 0; i < itemFiveAdapter.getCount(); i++) {
            BallItem ballItemFive = (BallItem) itemFiveAdapter.getItem(i);
            for (int j = 0; j < randomBallFive.size(); j++) {
                if (ballItemFive.getBallNum().equals(randomBallFive.get(j))) {
                    ballItemFive.setSelected(true);
                    codeFive.add(randomBallFive.get(j));
                    break;
                }
            }
        }

        //大小单双
        for (int i = 0; i < itemSixAdapter.getCount(); i++) {
            BallItem ballItemSix = (BallItem) itemSixAdapter.getItem(i);
            for (int j = 0; j < randomBallSix.size(); j++) {
                if (ballItemSix.getBallNum().equals(str[Integer.parseInt(randomBallSix.get(j))])) {
                    ballItemSix.setSelected(true);
                    codeSix.add(str[Integer.parseInt(randomBallSix.get(j))]);
                    break;
                }
            }
        }

        for (int i = 0; i < itemSevenAdapter.getCount(); i++) {
            BallItem ballItemSeven = (BallItem) itemSevenAdapter.getItem(i);
            for (int j = 0; j < randomBallSeven.size(); j++) {
                if (ballItemSeven.getBallNum().equals(str[Integer.parseInt(randomBallSeven.get(j))])) {
                    ballItemSeven.setSelected(true);
                    codeSeven.add(str[Integer.parseInt(randomBallSeven.get(j))]);
                    break;
                }
            }
        }


        countOne = randomBallOne.size();
        countTwo = randomBallTwo.size();
        countThree = randomBallThree.size();
        countFour = randomBallFour.size();
        countFive = randomBallFive.size();
        countSix = randomBallSix.size();
        countSeven = randomBallSeven.size();

        setNumberMoney(countOne, countTwo, countThree, countFour, countFive, countSix, countSeven);
    }

    //清空选中
    private void clearBall() {
        switch (modeState) {
            case 1:
                for (int i = 0; i < itemOneAdapter.getCount(); i++) {
                    BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
                    ballItemOne.setSelected(false);

                    BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(i);
                    ballItemTwo.setSelected(false);
                }
                break;
            case 2:
                for (int i = 0; i < itemOneAdapter.getCount(); i++) {
                    BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
                    ballItemOne.setSelected(false);

                    BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(i);
                    ballItemTwo.setSelected(false);

                    BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(i);
                    ballItemThree.setSelected(false);
                }
                break;
            case 3:
            case 7:
                for (int i = 0; i < itemOneAdapter.getCount(); i++) {
                    BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
                    ballItemOne.setSelected(false);

                    BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(i);
                    ballItemTwo.setSelected(false);

                    BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(i);
                    ballItemThree.setSelected(false);

                    BallItem ballItemFour = (BallItem) itemFourAdapter.getItem(i);
                    ballItemFour.setSelected(false);

                    BallItem ballItemFive = (BallItem) itemFiveAdapter.getItem(i);
                    ballItemFive.setSelected(false);
                }
                break;
            case 8:
                for (int i = 0; i < itemSixAdapter.getCount(); i++) {
                    BallItem ballItemSix = (BallItem) itemSixAdapter.getItem(i);
                    ballItemSix.setSelected(false);

                    BallItem ballItemSeven = (BallItem) itemSevenAdapter.getItem(i);
                    ballItemSeven.setSelected(false);
                }
                break;
            default:
                for (int i = 0; i < itemOneAdapter.getCount(); i++) {
                    BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
                    ballItemOne.setSelected(false);
                }
                break;
        }


        randomBallOne.clear();
        randomBallTwo.clear();
        randomBallThree.clear();
        randomBallFour.clear();
        randomBallFive.clear();
        randomBallSix.clear();
        randomBallSeven.clear();


        codeOne.clear();
        codeTwo.clear();
        codeThree.clear();
        codeFour.clear();
        codeFive.clear();
        codeSix.clear();
        codeSeven.clear();

        countOne = 0;
        countTwo = 0;
        countThree = 0;
        countFour = 0;
        countFive = 0;
        countSix = 0;
        countSeven = 0;

        itemOneAdapter.notifyDataSetChanged();
        itemTwoAdapter.notifyDataSetChanged();
        itemThreeAdapter.notifyDataSetChanged();
        itemFourAdapter.notifyDataSetChanged();
        itemFiveAdapter.notifyDataSetChanged();
        itemSixAdapter.notifyDataSetChanged();
        itemSevenAdapter.notifyDataSetChanged();

        //  tv_number_money.setText(Html.fromHtml("共0注，<font color=red>0</font>元"));
        tv_number_money.setVisibility(View.INVISIBLE);
    }


    int num = 0;
    int money = 0;

    //设置注数及金额
    private void setNumberMoney(int countOne, int countTwo, int countThree, int countFour, int countFive, int countSix, int countSeven) {

        Log.i(TAG, "countOne=" + countOne);
        Log.i(TAG, "countTwo=" + countTwo);
        Log.i(TAG, "countThree=" + countThree);
        Log.i(TAG, "countFour=" + countFour);
        Log.i(TAG, "countFive=" + countFive);
        Log.i(TAG, "countSix=" + countSix);
        Log.i(TAG, "countSeven=" + countSeven);

        switch (modeState) {
            //一星直选
            case 0:
                num = countOne * 1;
                money = num * 2;
                break;
            //二星直选
            case 1:
                num = countOne * countTwo;
                money = num * 2;
                break;
            //三星直选
            case 2:
                num = countOne * countTwo * countThree;
                money = num * 2;
                break;
            //五星直选 五星通选
            case 3:
            case 7:
                num = countOne * countTwo * countThree * countFour * countFive;
                money = num * 2;
                break;
            //二星组选
            case 4:
                switch (countOne) {
                    case 2:
                        num = 1;
                        money = num * 2;
                        break;
                    case 3:
                        num = 3;
                        money = num * 2;
                        break;
                    case 4:
                        num = 6;
                        money = num * 2;
                        break;
                    case 5:
                        num = 10;
                        money = num * 2;
                        break;
                    case 6:
                        num = 15;
                        money = num * 2;
                        break;
                }
                break;
            //组选三
            case 5:
                num = countOne * countOne - countOne;
                money = num * 2;
                break;
            //组选六
            case 6:
                switch (countOne) {
                    case 3:
                        num = 1;
                        money = num * 2;
                        break;
                    case 4:
                        num = 4;
                        money = num * 2;
                        break;
                    case 5:
                        num = 10;
                        money = num * 2;
                        break;
                    case 6:
                        num = 20;
                        money = num * 2;
                        break;
                    case 7:
                        num = 35;
                        money = num * 2;
                        break;
                    case 8:
                        num = 56;
                        money = num * 2;
                        break;
                }
                break;
            //大小单双
            case 8:
                num = countSix * countSeven;
                money = num * 2;
                break;
        }
        if (num > 0) {
            tv_number_money.setVisibility(View.VISIBLE);
            tv_number_money.setText(num + "注 , " + money + "元");
        } else {
            tv_number_money.setVisibility(View.INVISIBLE);
        }
        // tv_number_money.setText(Html.fromHtml("共" + num + "注，<font color=red>" + money + "</font>元"));

    }

    boolean isTo; //是否跳转
    boolean restrict; //投注限制

    //添加数据
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        restrict = false;
        isTo = false;

        if (event.getAction() == MotionEvent.ACTION_UP) {
            iv_add_selector.setBackgroundResource(R.drawable.add_off);
            if (v.getId() == R.id.rl_add || v.getId() == R.id.bt_bottom_right) {
                if (v.getId() == R.id.bt_bottom_right) {
                    isTo = true;
                    switch (modeState) {
                        case 1:
                            if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                                toActivity();
                            } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                                randomSelect();
                            } else {
                                restrict = true;
                            }
                            break;
                        case 2:
                            if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                                toActivity();
                            } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                                randomSelect();
                            } else {
                                restrict = true;
                            }
                            break;
                        case 3:
                        case 7:
                            if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0 && codeFour.size() == 0 && codeFive.size() == 0) {
                                toActivity();
                            } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0 && codeFour.size() == 0 && codeFive.size() == 0) {
                                randomSelect();
                            } else {
                                restrict = true;
                            }
                            break;
                        //大小单双
                        case 8:
                            if (ballNumber.size() > 0 && codeSix.size() == 0 && codeSeven.size() == 0) {
                                toActivity();
                            } else if (ballNumber.size() == 0 && codeSix.size() == 0 && codeSeven.size() == 0) {
                                randomSelect();
                            } else {
                                restrict = true;
                            }
                            break;
                        default:
                            if (ballNumber.size() > 0 && codeOne.size() == 0) {
                                toActivity();
                            } else if (ballNumber.size() == 0 && codeOne.size() == 0) {
                                randomSelect();
                            } else {
                                restrict = true;
                            }
                            break;
                    }
                } else {
                    restrict = true;
                }

                addAllToBetting();
            }
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (v.getId() == R.id.rl_add) {
                iv_add_selector.setBackgroundResource(R.drawable.add_on);
            }
        }
        return true;
    }

    private void addAllToBetting() {
        if (restrict) {
            switch (modeState) {
                case 0:
                    if (codeOne.size() > 0) {
                        addSelector(isTo);
                    }
                    break;
                case 1:
                    if (codeOne.size() > 0 && codeTwo.size() > 0) {
                        addSelector(isTo);
                    }
                    break;
                case 2:
                    if (codeOne.size() > 0 && codeTwo.size() > 0 && codeThree.size() > 0) {
                        addSelector(isTo);
                    }
                    break;
                case 3:
                case 7:
                    if (codeOne.size() > 0 && codeTwo.size() > 0 && codeThree.size() > 0 && codeFour.size() > 0 && codeFive.size() > 0) {
                        addSelector(isTo);
                    }
                    break;
                case 4:
                    if (codeOne.size() > 1) {
                        addSelector(isTo);
                    }
                    break;
                case 5:
                    if (codeOne.size() > 1) {
                        addSelector(isTo);
                    }
                    break;
                case 6:
                    if (codeOne.size() > 2) {
                        addSelector(isTo);
                    }
                    break;
                case 8:
                    if (codeSix.size() > 0 && codeSeven.size() > 0) {
                        addSelector(isTo);
                    }
                    break;
            }
        }
    }

    private void addSelector(boolean isTo) {

        formatBallNumber();
        tv_count.setVisibility(View.VISIBLE);
        tv_count.setText(String.valueOf(ballNumber.size()));

        if (isTo) {
            toActivity();
        }
        clearBall();
    }

    //格式化球数据
    private void formatBallNumber() {
        //不断生成销毁，数据由codeOnes决定
        StringBuffer strBuffer = null;
        StringBuffer strBufferTwo = null;
        StringBuffer strBufferThree = null;
        StringBuffer strBufferFour = null;
        StringBuffer strBufferFive = null;

        //大小单双
        StringBuffer strBufferSix = null;
        StringBuffer strBufferSeven = null;


        switch (modeState) {
            case 0:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    strBuffer.append(codeOne.get(j));
                }
                ballNumber.add(strBuffer.toString());
                mode.add("一星直选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 1;
                break;
            case 1:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    strBuffer.append(codeOne.get(j));
                }
                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    strBufferTwo.append(codeTwo.get(j));
                }
                ballNumber.add(strBuffer.toString() + "," + strBufferTwo.toString());
                //填充mode
                mode.add("二星直选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 2;
                break;
            case 2:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    strBuffer.append(codeOne.get(j));
                }
                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    strBufferTwo.append(codeTwo.get(j));
                }
                strBufferThree = new StringBuffer();
                for (int j = 0; j < codeThree.size(); j++) {
                    strBufferThree.append(codeThree.get(j));
                }
                ballNumber.add(strBuffer.toString() + "," + strBufferTwo.toString() + "," + strBufferThree.toString());
                //填充mode
                mode.add("三星直选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 3;
                break;
            //五星直选
            case 3:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    strBuffer.append(codeOne.get(j));
                }
                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    strBufferTwo.append(codeTwo.get(j));
                }
                strBufferThree = new StringBuffer();
                for (int j = 0; j < codeThree.size(); j++) {
                    strBufferThree.append(codeThree.get(j));
                }
                strBufferFour = new StringBuffer();
                for (int j = 0; j < codeFour.size(); j++) {
                    strBufferFour.append(codeFour.get(j));
                }
                strBufferFive = new StringBuffer();
                for (int j = 0; j < codeFive.size(); j++) {
                    strBufferFive.append(codeFive.get(j));
                }
                ballNumber.add(strBuffer.toString() + "," + strBufferTwo.toString() + "," + strBufferThree.toString() + "," + strBufferFour.toString() + "," + strBufferFive.toString());
                //填充mode
                mode.add("五星直选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 5;
                break;
            //二星组选
            case 4:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + ",");
                    }
                }
                ballNumber.add(strBuffer.toString());
                //填充mode
                mode.add("二星组选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 2;
                break;
            //组选三
            case 5:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + ",");
                    }
                }
                ballNumber.add(strBuffer.toString());
                //填充mode
                mode.add("组选三");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 2;
                break;
            //组选6
            case 6:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + ",");
                    }
                }
                ballNumber.add(strBuffer.toString());
                //填充mode
                mode.add("组选六");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 3;
                break;
            //五星通选
            case 7:
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    strBuffer.append(codeOne.get(j));
                }
                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    strBufferTwo.append(codeTwo.get(j));
                }
                strBufferThree = new StringBuffer();
                for (int j = 0; j < codeThree.size(); j++) {
                    strBufferThree.append(codeThree.get(j));
                }
                strBufferFour = new StringBuffer();
                for (int j = 0; j < codeFour.size(); j++) {
                    strBufferFour.append(codeFour.get(j));
                }
                strBufferFive = new StringBuffer();
                for (int j = 0; j < codeFive.size(); j++) {
                    strBufferFive.append(codeFive.get(j));
                }
                ballNumber.add(strBuffer.toString() + "," + strBufferTwo.toString() + "," + strBufferThree.toString() + "," + strBufferFour.toString() + "," + strBufferFive.toString());
                mode.add("五星通选");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                number = 5;
                break;
            //大小单双
            case 8:
                strBufferSix = new StringBuffer();
                for (int j = 0; j < codeSix.size(); j++) {
                    strBufferSix.append(codeSix.get(j));
                }
                strBufferSeven = new StringBuffer();
                for (int j = 0; j < codeSeven.size(); j++) {
                    strBufferSeven.append(codeSeven.get(j));
                }
                ballNumber.add(strBufferSix.toString() + "," + strBufferSeven.toString());
                mode.add("大小单双");
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
                break;
        }

        mBottomBar.setBettingNumber(totalNum);
    }

    private void toActivity() {

        Log.i(TAG, "type=" + type);
        Log.i(TAG, "totalMoney=" + totalMoney);
        Log.i(TAG, "totalNum=" + totalNum);
        for (int i = 0; i < ballNumber.size(); i++) {
            Log.i(TAG, "ballNumber=" + ballNumber.get(i));
            Log.i(TAG, "mode=" + mode.get(i));
            Log.i(TAG, "numMoney=" + numMoney.get(i));
        }


        Intent intent = new Intent(this, BetConfirmActivity.class);
        intent.putExtra("type", type); //彩种
        intent.putExtra("totalMoney", totalMoney); //总金额
        intent.putExtra("totalNum", totalNum); //总注数
        intent.putExtra("number", number);
        intent.putStringArrayListExtra("ballNumber", ballNumber); //球
        intent.putStringArrayListExtra("mode", mode); //玩法及模式
        intent.putStringArrayListExtra("numMoney", numMoney); //注数及金额
        startActivityForResult(intent, 0);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) return;
        switch (resultCode) {
            //继续选号
            case 0:
                ballNumber = data.getStringArrayListExtra("ballNumber");
                mode = data.getStringArrayListExtra("mode");
                numMoney = data.getStringArrayListExtra("numMoney");
                totalMoney = data.getIntExtra("totalMoney", totalMoney);
                totalNum = data.getIntExtra("totalNum", totalNum);
                if (ballNumber.size() > 0) {
                    tv_count.setVisibility(View.VISIBLE);
                    tv_count.setText(String.valueOf(ballNumber.size()));
                }
                mBottomBar.setBettingNumber(totalNum);
                break;
            //清除选号
            case 1:
                clearAllBetting();
                break;
            case -1:
                this.finish();
                break;
        }
    }

    private void clearAllBetting() {
        ballNumber.clear();
        mode.clear();
        numMoney.clear();
        totalMoney = 0;
        totalNum = 0;
        tv_count.setVisibility(View.GONE);
        mBottomBar.setBettingNumber(totalNum);
    }

    int topHeight = 0;

    public int measureListViewWrongHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        topHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        params.height = topHeight;
        listView.setLayoutParams(params);
        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }


    public void showMissData() {

        itemOneAdapter.showMissData();
        itemTwoAdapter.showMissData();
        itemThreeAdapter.showMissData();
        itemFourAdapter.showMissData();
        itemFiveAdapter.showMissData();

        itemSixAdapter.showMissData();
        itemSevenAdapter.showMissData();
        //显示遗漏
        mTvMissData.setText("隐藏遗漏");
        PreferenceUtil.setIsShowMissing(this, true);
    }

    public void hideMissData() {

        itemOneAdapter.hideMissData();
        itemTwoAdapter.hideMissData();
        itemThreeAdapter.hideMissData();
        itemFourAdapter.hideMissData();
        itemFiveAdapter.hideMissData();

        itemSixAdapter.hideMissData();
        itemSevenAdapter.hideMissData();
        //隐藏遗漏
        mTvMissData.setText("显示遗漏");
        PreferenceUtil.setIsShowMissing(this, false);
    }

    @Override
    protected void sendRandom(Intent intent) {
        super.sendRandom(intent);
        //机选
        int num = intent.getIntExtra("num", 0);
        if (num > 0) {
            clearAllBetting();
            //开始机选
            for (int i = 0; i < num; i++) {
                randomSelect();
                addSelector(false);
            }
            intent.setAction(Constant_Result.EXECRANDOMLOTTERYNUM);
            intent.putExtra("type", type); //彩种
            intent.putExtra("totalMoney", totalMoney); //总金额
            intent.putExtra("totalNum", totalNum); //总注数
            intent.putExtra("number", number);
            intent.putStringArrayListExtra("ballNumber", ballNumber); //球
            intent.putStringArrayListExtra("mode", mode); //玩法及模式
            intent.putStringArrayListExtra("numMoney", numMoney); //注数及金额
            sendBroadcast(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer=null;
        }
    }
}





















